package com.lishem.carl._06stackandqueue;

import java.util.Stack;

/**
 * https://leetcode.cn/problems/implement-queue-using-stacks/
 * <p>
 * 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）：
 * <p>
 * 实现 MyQueue 类：
 * <p>
 * void push(int x) 将元素 x 推到队列的末尾
 * <p>
 * int pop() 从队列的开头移除并返回元素
 * <p>
 * int peek() 返回队列开头的元素
 * <p>
 * boolean empty() 如果队列为空，返回 true ；否则，返回 false
 */
public class _1LetCode232_用栈实现队列 {

    class MyQueue {

        private Stack<Integer> stack;
        private Stack<Integer> help;

        public MyQueue() {
            stack = new Stack<>();
            help = new Stack<>();
        }

        public void push(int x) {
            stack.push(x);
        }

        public int pop() {
            if (help.isEmpty()){
                while (!stack.isEmpty()){
                    help.push(stack.pop());
                }
            }
            return help.pop();
        }

        public int peek() {
            if (help.isEmpty()){
                while (!stack.isEmpty()){
                    help.push(stack.pop());
                }
            }
            return help.peek();
        }

        public boolean empty() {
            return help.isEmpty() && stack.isEmpty();
        }
    }

}
